<?php
/**
 * @copyright Copyright (c) 2021 勾股工作室
 * @license https://opensource.org/licenses/Apache-2.0
 * @link https://www.gougucms.com
 */
 
declare (strict_types = 1);

namespace app\admin\controller;

use app\admin\BaseController;
use app\admin\model\Student as StudentModel;
use app\admin\validate\StudentValidate;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\View;

class Student extends BaseController

{
	/**
     * 构造函数
     */
	protected $uid;
	protected $model;
    public function __construct()
    {
        $this->model = new StudentModel();
		$this->uid = get_login_admin('id');
    }
    /**
    * 数据列表
    */
    public function datalist()
    {
    	
        if (request()->isAjax()) {
			
        	$param = get_params();
			$where = [];
			if(!empty($param['keywords'])){
				$where[] = ['name', 'like', '%' . $param['keywords'] . '%'];
			}

			//pr($param);
			if(!empty($param['end_time'])){
				$where[] = ['end_time', '>=', strtotime($param['end_time'])];
			}
			if(!empty($param['end_time2'])){
				$where[] = ['end_time', '<=', strtotime($param['end_time2'])];
			}
			$create_admin = $this->uid;
			if($create_admin!=1){
				$where[]=['create_admin', '=', $create_admin];
			}
			$where['is_delete'] =1;
            $list = $this->model->getStudentList($where,$param)->toArray();
            //$list2=$list;
            //pr($list);
            foreach($list['data'] as $key=>$val){
            	/*$list['data'][$key]['jiaofei_time']=date('Y-m-d',$val['jiaofei_time']);
            	$list['data'][$key]['end_time']=date('Y-m-d',$val['end_time']);
            	$fuwu_type = Db::name('type')->where(['id'=>$val['fuwu_type'],])->find();
            	$list['data'][$key]['fuwu_type']=$fuwu_type['title'];*/
            	if($val['nianji']>6){
            		$list['data'][$key]['nianji']='已毕业';
            	}elseif($val['nianji']<=0){
            		$list['data'][$key]['nianji']='未入学';
            	}else{
            		$nianji = Db::name('nianji')->where(['id'=>$val['nianji'],])->find();
            		$list['data'][$key]['nianji']=$nianji['nianji'];
            	}
            	

            	$create_admin = Db::name('admin')->where(['id'=>$val['create_admin'],])->find();
            	$list['data'][$key]['create_admin']=$create_admin['nickname'];
            	/*strtotime($val['create_time']);
            	pr(strtotime($val['create_time']));*/
            	$isNew = time()-strtotime($val['create_time']);
            	//pr($isNew);
            	if($isNew<= 86400*20){
            		$list['data'][$key]['is_new']=1;
            	}else{
            		$list['data'][$key]['is_new']=0;
            	}

            	//pr($nianji);
            }
            //pr($list);
            return table_assign(0, '', $list);
        }
        else{
        	//View::assign('param', $param);
            return view();
        }
    }

    /**
    * 添加
    */
    public function add()
    {
        if (request()->isAjax()) {		
			$param = get_params();	
			$param['create_admin'] =$this->uid;
            // 检验完整性
            try {
                validate(StudentValidate::class)->check($param);
            } catch (ValidateException $e) {
                // 验证失败 输出错误信息
                return to_assign(1, $e->getError());
            }
			if(isset($param["jiaofei_time"])){
					$param["jiaofei_time"]= $param["jiaofei_time"]?strtotime($param["jiaofei_time"]):0;
				}
				if(isset($param["end_time"])){
					$param["end_time"]= $param["end_time"]?strtotime($param["end_time"]):0;
				}
				
            $this->model->addStudent($param);
        }else{
			return view();
		}
    }
	

    /**
    * 编辑
    */
    public function edit()
    {
		$param = get_params();
		
        if (request()->isAjax()) {			
            // 检验完整性
            try {
                validate(StudentValidate::class)->check($param);
            } catch (ValidateException $e) {
                // 验证失败 输出错误信息
                return to_assign(1, $e->getError());
            }
			if(isset($param["jiaofei_time"])){
					$param["jiaofei_time"]= $param["jiaofei_time"]?strtotime($param["jiaofei_time"]):0;
				}
				if(isset($param["end_time"])){
					$param["end_time"]= $param["end_time"]?strtotime($param["end_time"]):0;
				}
			$student= Db::name('student')->where(['id'=>$param['id'],])->find();
			if($student['nianji'] != $param['nianji']){
				$insertData['student_id']=$student['id'];
				$insertData['nianji']=$student['nianji'];
				$insertData['banji']=$student['banji'];
				$insertData['fuwu_type']=$student['fuwu_type'];
				$insertData['feiyong']=$student['shoufei'];
				$insertData['create_time']=time();
				Db::name('nianji_log')->insert($insertData);
			}

            $this->model->editStudent($param);
        }else{
			$id = isset($param['id']) ? $param['id'] : 0;
			$detail = $this->model->getStudentById($id);
			if (!empty($detail)) {
				View::assign('detail', $detail);

				$nianji_log = Db::name('nianji_log')->where(['student_id'=>$id,])->select()->toArray();
				foreach($nianji_log as $key=>$val){
					$nianji= Db::name('nianji')->where(['id'=>$val['nianji'],])->find();
					$nianji_log[$key]['nianji'] =$nianji['nianji'];

					$student= Db::name('student')->where(['id'=>$val['student_id'],])->find();
					$nianji_log[$key]['name'] =$student['name'];

					$type= Db::name('type')->where(['id'=>$val['fuwu_type'],])->find();
					$nianji_log[$key]['fuwu_type'] =$type['title'];
				}
				//pr($nianji_log);
				View::assign('nianji_log', $nianji_log);
				return view();
			}
			else{
				throw new \think\exception\HttpException(404, '找不到页面');
			}			
		}
    }


    /**
    * 查看信息
    */
    public function read()
    {
        $param = get_params();
		$id = isset($param['id']) ? $param['id'] : 0;
		$detail = $this->model->getStudentById($id);
		if (!empty($detail)) {
			/*$nianji = Db::name('nianji')->where(['id'=>$detail['nianji'],])->find();
            $detail['nianji']=$nianji['nianji'];
            $fuwu_type = Db::name('type')->where(['id'=>$detail['fuwu_type'],])->find();
            $detail['fuwu_type']=$fuwu_type['title'];*/
            $create_admin = Db::name('admin')->where(['id'=>$detail['create_admin'],])->find();
            $detail['create_admin']=$create_admin['nickname'];

			View::assign('detail', $detail);
			$nianji_log = Db::name('nianji_log')->where(['student_id'=>$id,])->select()->toArray();
			foreach($nianji_log as $key=>$val){
				$nianji= Db::name('nianji')->where(['id'=>$val['nianji'],])->find();
				$nianji_log[$key]['nianji'] =$nianji['nianji'];

				$student= Db::name('student')->where(['id'=>$val['student_id'],])->find();
				$nianji_log[$key]['name'] =$student['name'];

				/*$type= Db::name('type')->where(['id'=>$val['fuwu_type'],])->find();
				$nianji_log[$key]['fuwu_type'] =$type['title'];*/
			}
			//pr($nianji_log);
			View::assign('nianji_log', $nianji_log);
			return view();
		}
		else{
			throw new \think\exception\HttpException(404, '找不到页面');
		}
    }

    /**
    * 删除
	* type=0,逻辑删除，默认
	* type=1,物理删除
    */
    public function del()
    {
        $param = get_params();
		$id = isset($param['id']) ? $param['id'] : 0;
		$type = isset($param['type']) ? $param['type'] : 0;

        $this->model->delStudentById($id,$type);
   }
}
